const App = getApp();
const Dialog = require('../../../components/dialog/dialog');
import areaList from '../../../utils/region.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList:areaList,
    is_read: false,
    disabled: false,
    level : 0,
    levelName : '',
    levelPopup:false,
    levelColumns:{},


    regionPopup:false,
    region: '',
    submsgSetting: {}, // 订阅消息配置
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    // 获取订阅消息配置
    _this.getSubmsgSetting();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    // 获取分销商申请状态
    _this.getApplyState();
  },

  /**
   * 获取订阅消息配置
   */
  getSubmsgSetting() {
    let _this = this;
    App._get('wxapp.submsg/setting', {}, (result) => {
      _this.setData({
        submsgSetting: result.data.setting
      });
    });
  },

  /**
   * 获取分销商申请状态
   */
  getApplyState() {
    let _this = this;
    App._get('user.regionalagent/apply', {
      referee_id: _this.getRefereeid()
    }, (result) => {
      let data = result.data;
      // 当前是否已经为分销商
      if (data.is_dealer) {
        wx.redirectTo({
          url: '../index/index'
        });
      }
      // 设置当前页面标题
      wx.setNavigationBarTitle({
        title: data.words.apply.title.value
      });
      data.isData = true;
      let gradeList = data.grade_list.data;
      let levelColumns = [];
      gradeList.forEach(item=>{
        levelColumns.push(item.name)
      })
      data['levelColumns'] = levelColumns;
      _this.setData(data);
    });
  },

  /**
   * 显示申请协议
   */
  toggleApplyLicense() {
    Dialog({
      title: '申请协议',
      message: this.data.license,
      selector: '#zan-base-dialog',
      isScroll: true, // 滚动
      buttons: [{
        text: '我已阅读',
        color: 'red',
        type: 'cash'
      }]
    }).then(() => {
      // console.log('=== dialog resolve ===', 'type: confirm');
    });
  },

  /**
   * 已阅读
   */
  toggleSetRead() {
    let _this = this;
    _this.setData({
      is_read: !this.data.is_read
    });
  },

  /**
   * 提交申请 
   */
  onFormSubmit(e) {
    let _this = this,
      values = e.detail.value;

    // 验证姓名
    if (!values.name || values.name.length < 1) {
      App.showError('请填写姓名');
      return false;
    }

    // 验证手机号
    if (!/^\+?\d[\d -]{8,12}\d/.test(values.mobile)) {
      App.showError('手机号格式不正确');
      return false;
    }
    // 验证代理类型
    if (!this.data.level) {
      App.showError('请选择代理等级');
      return false;
    }
    // 验证代理地区
    if (!this.data.region) {
      App.showError('请选择代理地区！');
      return false;
    }
    // 验证是否阅读协议
    if (!_this.data.is_read) {
      App.showError('请先阅读分销商申请协议');
      return false;
    }

    // 按钮禁用
    _this.setData({
      disabled: true
    });
    values.grade_id = this.data.level;
    values.region = this.data.region;
    // 数据提交
    App._post_form('user.regionalagent.apply/submit', values, () => {
      // 获取分销商申请状态
      _this.getApplyState();
    }, null, () => {
      // 解除按钮禁用
      _this.setData({
        disabled: false
      });
    });
  },

  /**
   * 去商城逛逛
   */
  navigationToIndex(e) {
    // 跳转到首页
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 订阅消息通知
   */
  onSubMsg() {
    let _this = this;
    let tmplItem = _this.data.submsgSetting.dealer.apply.template_id;
    if (tmplItem.length > 0) {
      wx.requestSubscribeMessage({
        tmplIds: [tmplItem],
        success(res) {},
        fail(res) {},
        complete(res) {},
      });
    }
  },
  onShowRegion:function(){
    this.setData({
      regionPopup: true
    })
  },
  onCancelRegion(event){
    this.setData({
      regionPopup: false
    })
  },
  onConfirmRegion(event){
    const { index , values  } = event.detail;
    let region = [];
    values.map(item => {
      region.push(item.name)
    })
    region = region.join('-');
    this.setData({
      region : region
    })
    this.onCancelRegion();
  },
  onShowLevel:function(){
    this.setData({
      levelPopup: true
    })
  },
  onCancelLevelPopup(){
    this.setData({
      levelPopup: false
    })
  },
  onConfirmLevelPopup(event) {
    const { picker, value, index } = event.detail;
    console.log(index)
    let level = this.data.grade_list.data[index]['grade_id'];
    let weight = this.data.grade_list.data[index]['weight'];
    this.setData({
      level : level,
      weight : weight <= 3 ? weight : 3 ,
      levelName :value
    })
    // 重置地区选择
    let regionSelect = this.selectComponent('#regionSelect');
    regionSelect.reset()
    this.onCancelLevelPopup();
  },
  /**
   * 修改地区
   */
  bindRegionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 获取推荐人id
   */
  getRefereeid() {
    return wx.getStorageSync('referee_id');
  },

})